<template>
	<el-dialog
		title="角色授权"
		v-model="visible"
		:width="300"
		destroy-on-close
		@closed="$emit('closed')"
		style="padding-top: 0px"
	>
		<el-form ref="dialogForm" label-width="300px" label-height="520px">
			<el-row>
				<div
					style="
						width: 290px;
						height: 500px;
						border: 1px solid black;
						overflow: scroll;
						overflow-y: scroll;
						overflow-x: hidden;
					"
				>
					<el-tree
						ref="treeRef"
						:data="treeData"
						show-checkbox
						default-expand-all
						node-key="id"
						highlight-current
						:props="defaultProps"
						@check="getChange"
					/>
				</div>
			</el-row>
			<el-row>
				<el-checkbox v-model="allChoice" @change="checkboxClick"
					>全选</el-checkbox
				>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button
				v-if="mode != 'show'"
				type="primary"
				:loading="isSaveing"
				@click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import publicjs from "@/api/model/publicjs";
import TreeUtils from "@/api/model/TreeUtils";
export default {
	emits: ["success", "closed"],
	data() {
		return {
			defaultProps: {
				children: "children",
				label: "name",
			},
			addNum: 1,
			allChoice: false,
			isIndeterminate: false, // 全选框是否在勾选状态
			visible: false,
			isSaveing: false,
			needSaveFlag: false,
			keepList: [""],
			roleCode: "",
			//表单数据
			treeData: [],
			//权限类型
			purviewType:"0",
		};
	},
	methods: {
		//显示,type=0:后台基础权限,type=1:后台业务权限,type=2:手机服务员权限,type=3:电脑点单权限,
		open(mode = "Add",type) {
			this.mode = mode;
			this.visible = true;
			this.purviewType=type
			return this;
		},
		checkboxClick(val) {
			let ids = [];
			this.treeData.forEach((item) => {
				ids.push(item.id);
			});
			if (val) {
				this.$refs.treeRef.setCheckedKeys(ids);
			} else {
				this.$refs.treeRef.setCheckedKeys([]);
			}
		},
		//表单提交方法
		async submit() {
			let purviewList = this.$refs.treeRef.getCheckedKeys();
			this.isSaveing = true;
			//得到的数据this.entityObj
			// eslint-disable-next-line no-debugger
			//debugger
			var head = new publicjs.head();
			publicjs.initHead(head, "save");
			var ed = new publicjs.entitydata(head);
			var eo = new publicjs.entityobj();
			eo.AttrList = {
				Name: "Role",
				DoType: "SetPurview", 
			};
			//eo.BeforSql = "Delete from SystemRolePurview where RoleCode='"+this.roleCode+"';";
			eo.ValueList = { Code: this.roleCode,PurviewType: this.purviewType };
			eo.SubObjectList = [];
			let subObjList = [];
			for (let i = 0; i < purviewList.length; i++) {
				let subeo = new publicjs.entityobj();
				subeo.AttrList = {
					Name: "RolePurview",
					DoType: "Add",
				};
				subeo.ValueList = {
					Code: this.roleCode + purviewList[i],
					PurviewId: purviewList[i],
					PurviewType:this.purviewType,
					RoleCode: this.roleCode,
				};
				subObjList.push(subeo);
			}

			eo.SubObjectList.push({
				Name: "RolePurview",
				SubObject: subObjList,
			});
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				console.log(returnValueStr);
				// eslint-disable-next-line no-debugger
				//debugger;
				//var returnValue = JSON.parse(returnValueStr);
				//alert(returnValue.opFlag);
				this.isSaveing = false;
				this.visible = false;
				this.$emit("fatherMethod");
			} catch (error) {
				this.loading = false;
				//this.emptyText = error.statusText;
				return false;
			}
		},

		getChange(checkedNode, checkedData) {
			//eslint-disable-next-line no-debugger
			debugger;
			console.log(
				"checkedNode:" +
					checkedNode +
					";checkedData:" +
					checkedData +
					";"
			);
			//最下级节点，可能是按钮
			if (checkedNode.children.length == 0) {
				let nodeId = checkedNode.id;
				let queryId = nodeId.substring(0, nodeId.length - 2) + "00";
				this.$refs.treeRef.setChecked(queryId,true)
				// let ids = this.$refs.treeRef.getCheckedKeys();
				// if (ids.find((element) => element === queryId)) {
				// 	//eslint-disable-next-line no-debugger
				// 	debugger;
				// 	this.$refs.treeRef.setChecked(queryId,true)
				// }
			}
		},

		//表单注入数据
		async setData(data) {
			//alert('aaa')
			//eslint-disable-next-line no-debugger
			//debugger;
			this.roleCode = data.Code;
			var head = new publicjs.head();
			publicjs.initHead(head, "query");
			// eslint-disable-next-line no-undef
			let ed = new publicjs.entitydata(head);
			var eop = new publicjs.entityobj();
			eop.AttrList = {
				Name: "Purview",
				UseDistinct: false,
				OrgCode: "",
				UseBackUp: false,
				UseGroup: false,
			};
			eop.ValueList = {
				Id: "id",
				Name: "name",
				ParentId: "parentid",
			};
			eop.QueryList = { State: "1",PurviewType: this.purviewType };
			ed.Data.Content.ObjectList.Object.push(eop);
			var eorp = new publicjs.entityobj();
			eorp.AttrList = {
				Name: "RolePurview",
				UseDistinct: false,
				OrgCode: "",
				UseBackUp: false,
				UseGroup: false,
			};
			eorp.ValueList = {
				PurviewId: "PurviewId",
			};
			eorp.QueryList = { RoleCode: data.Code };
			ed.Data.Content.ObjectList.Object.push(eorp);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				console.log(returnValueStr);
				// eslint-disable-next-line no-debugger
				//debugger;
				var returnValue = JSON.parse(returnValueStr);
				if (returnValue.Result.OpFlag == "1") {
					// eslint-disable-next-line no-debugger
					//debugger;
					let returnObj =
						returnValue.Result.Content.ObjectList.Object;
					let format = {
						id: "id", //菜单编号，作为树形查找id
						root: "00", //顶层节点的parentId是root
						pid: "parentid", //父节点标识字段为parentId
						child: "children", //构造出的树形子节点数组名为children
						sort: { field: "id", direct: 1 },
					}; //排序字段为sortNo，且正序
					//eslint-disable-next-line no-debugger
					debugger;
					let result = TreeUtils.treetool.format(
						returnObj[0].Rows.Row,
						format
					);
					this.treeData = result;
					let selectdata = [];
					for (let j = 0; j < returnObj[1].Rows.Row.length; j++) {
						selectdata.push(returnObj[1].Rows.Row[j].PurviewId);
					}
					this.$refs.treeRef.setCheckedKeys(selectdata, false);
					ed = null;
					eop = null;
					eorp = null;
					head = null;
				}
				//this.summary = response.summary || {};
				this.loading = false;
				//this.$refs.scTable.setScrollTop(0);
				//this.$emit('dataChange', res, this.tableData)
			} catch (error) {
				this.loading = false;
				//this.emptyText = error.statusText;
				return false;
			}
		},
	},
	async mounted() {},
};
</script>

<style></style>
